package com.tsll.service.impl;


import com.tsll.dao.StaffMapper;
import com.tsll.dao.StaffsalMapper;
import com.tsll.entity.Staff;
import com.tsll.entity.Staffsal;
import com.tsll.service.StaffService;
import com.tsll.vo.EchartsVo;
import com.tsll.vo.Result;
import com.tsll.vo.StaffVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;


@Service
public class StaffServiceImpl implements StaffService {
    @Autowired
    private StaffMapper staffMapper;
    @Autowired
    private StaffsalMapper staffsalMapper;
    @Autowired
    private HttpSession session;


    @Override
    public Result selectname(String name, String password) {
        Staff staff = staffMapper.selectmima(name, password);
        if (staff == null) {
            return new Result(500, "密码或账号错误", null);
        }
        session.setAttribute("staff", staff);
        return new Result(200, "登录成功", null);

    }

    //查询员工
    public Result findByPage(Integer current, Integer pageSize, StaffVo staffVo) {
        //获取当前登录者的信息
        Staff staff = (Staff) session.getAttribute("staff");
        //启动分页
        PageHelper.startPage(current, pageSize);
        List<Staff> list = staffMapper.selectByVo(staffVo);
        //封装到PgeInfo
        PageInfo<Staff> pageInfo = new PageInfo<Staff>(list);
        return new Result(200, "查询员工信息成功", pageInfo);
    }

    @Override
    public List<Staff> findBy(Staff staff) {
        return staffMapper.selectaaa(staff);
    }

    //删除员工表
    @Override
    public Result delete(Integer id) {
        staffMapper.deleteByPrimaryKey(id);
        return new Result(200, "删除成功", null);
    }


    //查询工资表
    @Override
    public Result all(Integer current, Integer pageSize, StaffVo staffVo) {
        PageHelper.startPage(current, pageSize);
        List<Staff> staff = staffMapper.selectAlls(staffVo);
        PageInfo<Staff> pageInfo = new PageInfo<Staff>(staff);
        return new Result(200, "查询员工工资成功", pageInfo);
    }

    @Override
    public int deleteByPrimaryKey(int id) {
        return staffMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(Staffsal staffsal) {
        staffsalMapper.updateByPrimaryKeySelective(staffsal);
        return 0;
    }


    //添加员工表
    @Override
    public int addstaff(Staff staff) {
        return staffMapper.addstaff(staff);
    }

    @Override
    public int updatesatff(Staff staff) {
        return staffMapper.updateByPrimaryKeySelective(staff);
    }

    //查询离职员工
    @Override
    public Result lizhi(Integer current, Integer pageSize, StaffVo staffVo) {
        Staff staff = (Staff) session.getAttribute("staff");
        //启动分页
        PageHelper.startPage(current, pageSize);
        List<Staff> list = staffMapper.selectlizhi(staffVo);
        //封装到PgeInfo
        PageInfo<Staff> pageInfo = new PageInfo<Staff>(list);
        return new Result(200, "查询离职员工信息成功", pageInfo);
    }

    @Override
    public List<EchartsVo> selectname() {
        List<EchartsVo> selectname = staffMapper.selectname();
        return selectname;
    }
//    查询密码
    public int updateLogin(String newpass,Integer id){

        return  staffMapper.updateLogin(newpass,id);
    }

    @Override
    public Result getstaffshu() {

        return new Result(200,"查询成功", staffMapper.getstaffshu());
    }

    //离职
    public Result updateById(@Param("id")Integer id){
         staffMapper.updateById(id,0,null);
        return new Result(200,"离职成功",null);
    }

    @Override
    public Result selectByStaffid(Integer id) {

        Staff staff = staffMapper.selectByStaffid(id);
        if(staff!=null){
            return new Result(500,"员工编号重复请重新输入",null);
        }
        return new Result(200,"",null);
    }
}

